package com.jk.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.jk.blog.common.Constant;
import com.jk.blog.config.redis.RedisService;
import com.jk.blog.config.security.WebSecurityService;
import com.jk.blog.entity.AccountInfo;
import com.jk.blog.entity.UserInfo;
import com.jk.blog.mapper.basic.UserInfoMapper;
import com.jk.blog.model.ResponseInfo;
import com.jk.blog.service.AccountService;
import com.jk.blog.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class AccountServiceImpl implements AccountService {

    private static final Logger log = Logger.getLogger(AccountServiceImpl.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private WebSecurityService webSecurityService;

    @Override
//    @Cacheable(value = "USER_SESSION", keyGenerator = "wiselyKeyGenerator")
    public ResponseInfo queryLoginUserInfo(String loginAccount, String password) {
        if(StringUtils.isEmpty(loginAccount) || StringUtils.isEmpty(password)){ //账户或密码为空
            return ResponseInfo.ACCOUNT_PARAMETER_ERROR;
        }
        AccountInfo account = new AccountInfo();
        // 登录账号
        account.setLoginAccount(loginAccount);
        // 登录密码：MD5加密
        account.setPassword(StringUtils.toMD5(password));
        // 用户账号登录验证，成功返回用户信息
        UserInfo userInfo = userInfoMapper.selectLoginUserInfo(account);
        if(userInfo == null){ //用户信息返回为空，账号或密码有误
            return ResponseInfo.ACCOUNT_ERROR;
        }
        // 登录信息Redis缓存
        redisService.set(Constant.LOGIN_SESSION_KEY_PREFIX + userInfo.getAccountId(), JSON.toJSONString(userInfo));

//        webSecurityService.loadUserByUsername(loginAccount);
        return new ResponseInfo(ResponseInfo.SUCCESS, userInfo);
    }

    @Override
    @Transactional
    public ResponseInfo addRegisterInfo(String loginAccount, String loginEmail, String password) {
        ResponseInfo res;
        try {
            // 新增登录账号信息
            AccountInfo account = new AccountInfo();
            account.setLoginAccount(loginAccount); // 登录账号
            account.setLoginEmail(loginEmail); // 登录邮箱
            account.setPassword(StringUtils.toMD5(password)); // 登录密码，MD5加密
            userInfoMapper.insertAccount(account);

            // 新增用户信息
            UserInfo user = new UserInfo();
            // 账号ID
            user.setAccountId(account.getId());
            userInfoMapper.insertUserInfo(user);

            res = ResponseInfo.SUCCESS;
        } catch (Exception e) {
            res = ResponseInfo.ERROR;
        }
        return res;
    }

    @Override
    public ResponseInfo queryAccountDetail(Integer id) {
        ResponseInfo res;
        try {
            AccountInfo account;
            Date first = new Date();
            account = userInfoMapper.selectAccountDetail(id);
            log.info("first quest costs:"+ (new Date().getTime()-first.getTime()) +" ms");

            Date second = new Date();
            account = userInfoMapper.selectAccountDetail2(id);
            log.info("second quest costs:"+ (new Date().getTime()-second.getTime()) +" ms");

            res = new ResponseInfo(ResponseInfo.SUCCESS, account);
        } catch (Exception e) {
            res = ResponseInfo.ERROR;
            log.error(e.getMessage(), e);
        }
        return res;
    }

}
